/**
 * @Controller 文章
 */
const Controller = require('egg').Controller;
const { ARTICLE_ID, ARTICLE_PREVIEW, ARTICLE_SAVE } = require('../constant/redis');

class ArticlesController extends Controller {

    async index() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.article.getArticles(ctx.query)
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ``
            };
        }
        catch (err) {
            throw err;
        }
    }


    /**
     * @Router GET /api/v1/articles/:id
     * @Request header string token
     * @Response 200 baseResponse 响应
     * @Summary 预览
     */
    async show() {
        const ctx = this.ctx;
        const id = ctx.params.id;
        try {
            let data;
            if (ctx.query && ctx.query.action == 1) {
                data = await ctx.service.article.getArticleDetail(id)
            } else {
                data = await ctx.service.cache.get([ARTICLE_SAVE, id])
            }
            if(data) {
                ctx.body = {
                    code: 200,
                    data,
                    success: true,
                    msg: ``
                };
            } else {
                ctx.status = 403
                ctx.body = {
                    code: 403,
                    data: {},
                    success: false,
                    msg: `预览已失效，请重新生成！`
                };
            }
        }
        catch (err) {
            throw err;
        }
    }

    /**
     * @Router POST /api/v1/articles
     * @Request header string token
     * @Request body string data 请求数据
     * @Response 200 baseResponse 响应
     * @Summary 提交预览
     */
    async create() {
        const { ctx } = this;
        try {
            if (ctx.request.body && ctx.request.body.action === 1) {
                const data = await ctx.service.article.createArticle(ctx.request.body)
                ctx.body = {
                    code: 200,
                    data: {
                        id: data._id
                    },
                    success: true,
                    msg: '',
                }
            } else {
                let { articleId } = ctx.request.body;
                if (!articleId) {
                    articleId = await ctx.service.cache.incr([ARTICLE_ID]);
                }
                await ctx.service.cache.set([ARTICLE_SAVE, articleId], ctx.request.body, ctx.app.constant.ACCESS_TOKEN_EX * 60 * 60)
                ctx.body = {
                    code: 200,
                    data: {
                        id: articleId
                    },
                    success: true,
                    msg: '',
                }
            }
        } catch (error) {
            throw error;
        }
    }

    
    async destroy() {
        const { ctx } = this;
        try {
            const result = await ctx.service.article.deleteArticle(ctx.params.id);
            if (result && result._id) {
                ctx.body = {
                    code: 200,
                    data: {},
                    success: true,
                    msg: '删除成功'
                }
            } else {
                throw new Error('删除失败')
            }
        } catch (error) {
            throw error;
        }
    }

    async edit() {
        const { ctx } = this;
        try {
            const result = await ctx.service.article.editArticle(ctx.params.id);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

    async update() {
        const { ctx } = this;
        try {
            const result = await ctx.service.article.updateArticle(ctx.params.id, ctx.request.body);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

    async publish() {
        const { ctx } = this;
        try {
            const result = await ctx.service.article.publishArticle(ctx.params.id);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

}

module.exports = ArticlesController;
